let app = getApp();

Page({
  data: {
    info: [],
    info2: [],
    serList: [],
    ids: []
  },
  onLoad() {
    this.getData();
  },
  getData() {
    wx.request({
      url: 'http://localhost:8080/yrdj/oneType/list',
      method: 'GET',
      success: (res) => {
        this.setData({
          info: res.data.rows
        });
        this.bianli();
      }
    });
  },

  bianli() {
    this.setData({
      info2: this.data.info.map(item => {
        return {
          name: item.title,
          value: item.id,
          checked: false
        };
      })
    });
    wx.request({
      url: 'http://localhost:8080/experience/experience/listAll',
      method: "GET",
      data: {
        "masterId": app.globalData.userID
      },
      success: (res) => {
        if (res.data.code == 200) {
          this.setData({
            serList: res.data.data
          })
          let set = []
          for (let i = 0; i < this.data.serList.length; i++) {
            set.push(this.data.serList[i].serviceCategoryId)
            this.setData({
              ids: set
            })
            for (let j = 0; j < this.data.info2.length; j++) {
              if (this.data.info2[j].value == this.data.serList[i].serviceCategoryId) {
                let list = this.data.info2
                list[j].checked = true
                this.setData({
                  info2: list
                })
              }
            }
          }
        }
      }
    })
  },

  onCheckboxChange(e) {
    const values = e.detail.value;
    console.log(values);
    this.setData({
      ids: values
    });
  },
  onConfirm() {

    wx.request({
      url: 'http://localhost:8080/experience/experience/clearAll?masterId=' + app.globalData.userID,
      method: "GET",
      success: (res) => {
        if (res.data.code == 200) {
          console.log(this.data.ids);
          console.log(app.globalData.userID);
          wx.request({
            url: 'http://localhost:8080/experience/experience/listIds',
            method: "POST",
            data: {
              serviceCategoryIds: this.data.ids,
              masterId: app.globalData.userID
            },
            success: (res) => {
              if (res.data.code == 200) {
                wx.navigateBack({
                  url: '/packageMy/pages/completeDocumentation/completeDocumentation?',
                })
              }
            }
          })
        }
      }
    })

  }
})